The file lock seems simple, but it has encountered many problems in Android. After several attempts, we finally come to the correct solution:
First, let's look at how to get the filelock:
Filechannel. trylock ()
To obtain the file lock, first open the file channel. The channel can come from several places. I tried three places:
1. randomaccessfile
If the file does not exist, it will be created on its own. It is found that the file lock can be obtained
recently in the flume part of the function of the source code, about the use of filelock, in fact, a lot of open source framework has involved, I have seen the lucene,zookeeper,hadoop,es and other open source framework are useful to the following simple introduction of the next Filelock. 1,filelock is an exclusive lock that controls concurrent access to the sa
The use of filelock in a multithreaded environment to achieve the lock function of the file, to avoid multiple threads simultaneously operating the same file,
Program:
public class demo{
Filelock lock;
/* Read/write file * *
Public Rwfile () {
Lock=trylock ();
if (lock!=null) {
Lock succeeded
You can work with the file.
Unlock ();
}else{
Lock failure
}
}
/* Lock operation return NON-EMPTY = Lock Success
Filelock is a file lock, process lock, for inter-process concurrency, which controls concurrent access by different programs (JVMS) to the same file.Filelock is a Java 1.4 version of a class, it can be a writable file (W) lock, to ensure that only one process can get the file lock, the process so that the file can be accessed, and the other cannot get the lock process or choose to be suspended waiting, Or choose to do something else, a mechanism that
Principle:The Filelock in the JDK's NIO package implements a file lock similar to the Linux fcntl , allowing the file to be accessed by the process mutex. With this feature, a powerful Java process mutex can be implemented to ensure that only the unique jar application process is running at the same time at the application level ! avoid certain factors that cause the jar to execute repeatedly, competing with multiple processes, and destroying business
In a multi-threaded program, if a shared file is to be accessed or edited, how can I read and write files in the exclusive mode?
Using java. NiO. channels. filelock is a good method. The idea is to first create a file named genfile. LCK,
The getchannel (). trylock () method returns a filelock. If no one accesses the file, a reference not empty is returned,
If someone is accessing the service, null is ret
lock.Here we will mainly talk about "LOCK_EX" and "LOCK_NB ".For example, we have two files.Flocka. php
? View Code PHP1 2 3 4 5 6 7 8 9 10$ File = 'temp.txt '; $ fp = fopen ($ file, 'A'); for ($ I = 0; $ I
Flockb. php
? View Code PHP1 2 3 4 5 6 7 8 9$ File = 'temp.txt '; $ fp = fopen ($ file, 'A'); for ($ I = 0; $ I Run flocka. php first, and then run flockb. php immediately.Result:11111111222222222222222222222222222222222222222211111111111111111111111111111111This indicates that when no
# Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include # Include
Int m_ncountmember;Vector vectdata;Inline void putint (string & STR, int ncount){Char szbuffer [1024] = {0
Scenario 1:A single-process program can be bound with a port. When the program starts, you can try to see if the port is already in use, and if so, the program has started.Scenario 2:You can create a hidden file in a Java program and delete the file when the program exits. This way, when the program starts, you determine if the file exists, if the description is already started.Import Java.io.file;import java.io.ioexception;import Java.io.randomaccessfile;import java.nio.channels.FileLock;/ *
Sometimes the following message is displayed when you open a file: the file has beenProgramUsage. Opening failed. This is because another program is editing the file and does not want other programs to modify the file during the editing process, thus locking the file.
In Java, the filelock class is used to lock files.CodeThis method is demonstrated.
Package fileoperation; import Java. io. fileoutputstream; import Java. io. ioexception; import Java.
-jar xxx. jar.The Java Startup Program does not have A single instance limitation similar to C #. You can start multiple programs, but you cannot start multiple programs. You cannot have multiple daemon A to daemon the server program, what if I start multiple server programs?
Ii. technical explanationHere the technical explanation is relatively rough. Please refer to Baidu for details. Here we will only explain the function.1. jps command.The command tool that comes with JDK. You can use jps-l t
equivalent to starting a program using java-jar xxx. jar. The Java Startup Program does not have A single instance limitation similar to C #. You can start multiple programs, but you cannot start multiple programs. You cannot have multiple daemon A to daemon the server program, what if I start multiple server programs? Ii. technical explanation the technical explanation here is rough. Please refer to Baidu for details. Here we will only explain the functions. 1. jps command. The command tool th
destination of the data. It is used to provide data to the buffer or read the buffer data, and provides asynchronous support for I/O.
(2)Structure of the channel class diagram?
Java. NIO. channels. A channel is a public interface, which is implemented by all sub-channels. NIO. the channels package also provides good classes such as channels, filelock, selectionkey, selector, and pipe. It contains socket, file, and pipe pipelines, which are actually
);System.out.println (Randomaccessfile.getfilepointer ()); Print 200Second, file lock (Filelock)As we all know, file locks are divided into exclusive and shared locks. An exclusive lock is when a lock has the permission to operate the file, and the other visitors must wait for the current operator to complete the ability to manipulate the file, while the shared lock is all the visitors can use the file at the same time.Exclusive locks are most importa
. Io. fileoutputstream;
Import java. NiO. channels. filelock;
Import java. util. Concurrent. timeunit;
/**
* It mainly studies the APIs of trylock () and lock.
* Full-text locking without parameters supports changing the file size,
* Partial locking with a fixed size will not automatically adapt to file growth (
* If a locked region initially contains the end Of the file
* And the file grows beyond the region then the new portion of the file
can use RMI or CORBA. In fact, Java corab is also implemented through RMI, and RMI is ultimately implemented using socket. Therefore, socket is the most basic means of inter-process communication in Java.
The inprocess routine implements inter-process communication by means of shared memory.
Simple diagram of inprocess communication between processes
Import Java. io. ioexception; import Java. io. randomaccessfile; import Java. NIO. mappedbytebuffer; import Java. NIO. channels. filechannel; i
);
public Final Filelock lock ();
Public Abstract Filelock Lock (long position, long size, Boolean shared);
public final Filelock Trylock ();
public abstract Filelock Trylock (long position, long size, Boolean shared);
Public Abstract Mappedbytebuffer map (mapmode mode, long position, long size);
p
read/write content will be wrong-note that only the content is wrong and the operation will not go wrong. After the lock is applied, some operations are rejected in some cases. Filelock does not protect files and data, but ensures data synchronization. Therefore, filelock is truly effective only for users who hold locks, only when all users use the same method to operate on files using the
This article illustrates the 5 ways to solve Java exclusive writing files, including some of their own understanding, if there is no place to welcome the proposed.
Scenario 1: Use the randomaccessfile file action option S,s to represent the synchronous lock mode write
Randomaccessfile file = new Randomaccessfile (file, "RWS");
Scenario 2: using the filechannel file lock
File File = new file ("Test.txt");
FileInputStream fis = new FileInputStream (file);
FileChannel channel
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.